/*
 * Powered By 
 * Since 2010 - 2016
 */

package com.rf.business.control;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;
import com.rf.business.model.CarArchives;
import com.rf.business.service.CarArchivesService;
import com.rf.sys.annotation.SystemLog;
import com.rf.sys.frm.BaseController;
import com.rf.sys.model.User;
import com.rf.sys.service.CommonService;
import com.rf.sys.service.DepUserService;
import com.rf.sys.service.UserService;
import com.rf.sys.utils.ConfigUtils;
import com.rf.sys.utils.DateUtils;
import com.rf.sys.utils.JsonUtils;
import com.rf.sys.utils.PageUtils;
import com.rf.sys.utils.UuidUtil;

@Controller
@RequestMapping("/bus/carArchives")
public class CarArchivesController extends BaseController{
	/**
	 * 日志
	 */
	@Resource
	private CarArchivesService carArchivesService;
	@Resource
	private CommonService commonService;
	@Resource
	private UserService userService;
	@Resource
	private DepUserService depUserService;
	/**
	 * 初始化页面信息
	 * @param request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
	 */
	@RequestMapping(value = "index")
    public ModelAndView index (HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/business/carArchives/list");
		return modelAndView; 
    }
    
    /**
	 * 搜索页面信息
	 * @param Page,request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "query")
    @ResponseBody
    public void query (PageUtils pageUtils, CarArchives carArchives, HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	//获取分页、查询参数，进行数据封装
    	Map searchMap = carArchivesService.getCarArchivesPage(carArchives, pageUtils);
    	super.responsePrint(request, response, JsonUtils.mapToJson(searchMap));
    }
    
    /**
     * 编辑页面初始化
     * @param request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
     */
    @RequestMapping(value = "edit")
    public ModelAndView edit (HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	ModelAndView modelAndView = new ModelAndView();
		String id = request.getParameter("id");
		CarArchives carArchives = new CarArchives();
		if(id != null && !id.equals("")){
			carArchives = carArchivesService.getCarArchivesByID(id);
			modelAndView.addObject("type", "2");
			String prodecedTime = DateUtils.dateToString(carArchives.getProdecedTime(), ConfigUtils.DATEFORMATER2);
			String beginDate = DateUtils.dateToString(carArchives.getBeginDate(), ConfigUtils.DATEFORMATER2);
			String endDate = DateUtils.dateToString(carArchives.getEndDate(), ConfigUtils.DATEFORMATER2);
			modelAndView.addObject("beginDate", beginDate);
			modelAndView.addObject("endDate", endDate);
			modelAndView.addObject("prodecedTime", prodecedTime);
		}else{
			//新增时初始化ID
			carArchives.setId(UuidUtil.get32UUID());
			carArchives.setSerialNo(commonService.getSerialNo(3));//流水号
			carArchives.setArchiveNo(commonService.getSerialNo(4));//海关车辆备案号
			modelAndView.addObject("type", "1");
		}
		modelAndView.addObject("carArchives", carArchives);
        modelAndView.setViewName("/business/carArchives/edit");
        return modelAndView; 
    }
    
    /**
     * 保存数据信息
     * @param CarArchives,request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
     */
    @RequestMapping(value = "save")
    @SystemLog(module="业务功能",methods="车辆备案管理-保存车辆备案")
    public void save (HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	JSONObject returnObj = new JSONObject();
    	//获取当前登陆用户信息
    	Session session = SecurityUtils.getSubject().getSession();
		User user = (User)session.getAttribute("userSession");
		
    	String params = request.getParameter("data");
    	Gson gson = new Gson();
    	CarArchives carArchives = gson.fromJson(params, CarArchives.class);
		carArchives.setState(ConfigUtils.ENABLESTATE);
		carArchives.setCreateUser(user.getId());
		carArchives.setCreateTime(new Date());
		carArchivesService.addCarArchives(carArchives);
		
		returnObj.element("message", "success");//成功
		super.responseJson(request, response, returnObj.toString());
    }
    
    /**
     * 保存数据信息
     * @param CarArchives,request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
     */
    @RequestMapping(value = "update")
    @SystemLog(module="业务功能",methods="车辆备案管理-更新车辆备案信息")
    public void update (HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	JSONObject returnObj = new JSONObject();
    	String params = request.getParameter("data");
    	Gson gson = new Gson();
    	CarArchives carArchives = gson.fromJson(params, CarArchives.class);
		//修改
		carArchivesService.updateCarArchives(carArchives);
		returnObj.element("message", "success");//成功
		super.responseJson(request, response, returnObj.toString());
    }
    
    /**
     * 删除数据信息
     * @param request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "delete")
    @ResponseBody
    @SystemLog(module="业务功能",methods="车辆备案管理-删除车辆备案信息")
    public void delete (HttpServletRequest request, HttpServletResponse response) throws Exception{
    	JSONObject returnObj = new JSONObject();
    	String ids = request.getParameter("ids");
    	carArchivesService.deleteCarArchives(ids);
		returnObj.element("message", "success");//成功
		super.responseJson(request, response, returnObj.toString());
    }
    
    /**
     * 更新数据状态
     * @param carArchives,request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "updateState")
    @ResponseBody
    @SystemLog(module="业务功能",methods="车辆备案管理-更新车辆备案信息状态")
    public void updateState (CarArchives carArchives, HttpServletRequest request, HttpServletResponse response) throws Exception{
    	JSONObject returnObj = new JSONObject();
    	carArchivesService.updateCarArchives(carArchives);
		returnObj.element("message", "success");//成功
		super.responseJson(request, response, returnObj.toString());
    }
}


